package GUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.io.File;
import java.util.List;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import Model.Conference;
import Model.Paper;
import Model.User;

/**
 * Panel that displays papers assigned to the Review
 * accessing the tool for a specific conference.
 * 
 * Allows a Review to Submit/Edit (re-submit) a 
 * review form (File) for the papers assigned.
 * 
 * @author Paul Schultz (ps87@uw.edu)
 * @version 12/02/2012
 */
@SuppressWarnings("serial")
public class ReviewerToolPanel extends JPanel
{
  
  private Conference my_conference;
  private User my_user;
  private ConferenceWizardFrame my_frame;
  /**
   * Constructs new ReviewerTool Panel.
   * 
   * @param the_conf Conference data used to populate panel.
   * @param the_user User data used to populate panel.
   * @param the_frame data used change current (viewed) panel.
   */
  public ReviewerToolPanel(final Conference the_conf, final User the_user, 
      final ConferenceWizardFrame the_frame)
  {
    super();
   
    my_conference = the_conf;
    my_user = the_user;
    my_frame = the_frame;
    
    setLayout(new BorderLayout());
    setPreferredSize(new Dimension(550, 600));
    setBackground(Color.WHITE);
    
    JLabel header = new JLabel("Reviewer Tool");
    header.setForeground(Color.BLUE);
    header.setFont(new Font("Plain", Font.BOLD, 38));
    
    JPanel title = new JPanel(new FlowLayout(FlowLayout.LEADING));
    title.setBackground(Color.WHITE);
    title.add(header);
    
    JPanel instruct_panel = new JPanel(new FlowLayout(FlowLayout.LEADING));
    instruct_panel.setBackground(Color.WHITE);
    
    JPanel north_panel = new JPanel();
    north_panel.setLayout(new BoxLayout(north_panel, BoxLayout.Y_AXIS));
    north_panel.setBackground(Color.WHITE);
    
    north_panel.add(title);
    north_panel.add(Box.createVerticalStrut(10));
    north_panel.add(instruct_panel);
    
    add(north_panel, BorderLayout.NORTH);
    
    JPanel papers_panel = new JPanel();
    papers_panel.setLayout(new BoxLayout(papers_panel, BoxLayout.Y_AXIS));
    papers_panel.setBackground(Color.WHITE);
    
    JLabel pLabel = new JLabel("Assigned Papers:");
    pLabel.setFont(new Font("Plain", Font.PLAIN, 28));
    pLabel.setForeground(new Color(0, 102, 204));
    
    papers_panel.add(pLabel);
    papers_panel.add(Box.createVerticalStrut(20));
    
    List<Model.Paper> papers = the_conf.getPapersAssignedToReviewers(the_user);
    
    for (Model.Paper p : papers) 
    {
      papers_panel.add(new ReviewPaperPanel(p, the_conf, the_user, the_frame));
      papers_panel.add(Box.createVerticalStrut(10));
    }
    
    JScrollPane scroll_panel = new JScrollPane(papers_panel);
    add(scroll_panel, BorderLayout.CENTER);
  }
  
  /**
   * Class used for constructing JPanels containing
   * important paper information and buttons for
   * adding/editing a review File for a paper.
   * 
   * @author Paul Schultz (ps87@uw.edu)
   * @version 12/02/2012
   */
  public class ReviewPaperPanel extends JPanel
  {
    
    /**
     * Constructs a new ReviewPaperPanel.
     * 
     * @param the_paper The paper that the review is for.
     * @param the_conf The conference the paper is in.
     * @param the_user The Reviewer submitting reviews.
     * @param the_frame The master frame for the application.
     */
    public ReviewPaperPanel(final Paper the_paper, final Conference the_conf, 
        final User the_user, final ConferenceWizardFrame the_frame)
    {
      super();
      this.setAlignmentX(LEFT_ALIGNMENT);
      GridLayout gLayout = new GridLayout(0,2);
      //gLayout.setHgap(80);
      this.setLayout(gLayout);
      JLabel titleLabel = new JLabel("Title: " + the_paper.getTitle());
      titleLabel.setFont(new Font("Plain", Font.PLAIN, 18));
      titleLabel.setAlignmentX(LEFT_ALIGNMENT);
      
      JLabel authorLabel = new JLabel("Author: " + the_paper.getAuthor().realName());
      authorLabel.setFont(new Font("Plain", Font.PLAIN, 16));
      authorLabel.setForeground(new Color(0, 0, 102));
      
      JPanel button_panel = new JPanel();
      
      JButton subReview = new JButton();
      subReview.addActionListener(new AddReviewListener(the_paper, the_user, the_frame));
      JLabel reviewFile = new JLabel();
      reviewFile.setFont(new Font("Plain", Font.PLAIN, 16));
      
      File review = the_paper.getMyReviewAssociation().get(the_user);
      
      // If review already exists, give edit button.
      if (review != null) 
      {
        subReview.setText("Edit Review");
        reviewFile.setText(review.getName());
        reviewFile.setForeground(new Color(0, 153, 51));
        
      }
      // Else give submit button for new review.
      else
      {
        subReview.setText("Submit Review");
        reviewFile.setText("No File Uploaded.");
        reviewFile.setForeground(Color.RED);
      }
      JLabel fileHeader = new JLabel("Review File:");
      fileHeader.setFont(new Font("Plain", Font.PLAIN, 16));
      
      JPanel rFilePanel = new JPanel(new FlowLayout(FlowLayout.LEADING));
      rFilePanel.add(fileHeader);
      rFilePanel.add(reviewFile);
      
      JLabel spacer = new JLabel("");
      spacer.setFont(new Font("Plain", Font.PLAIN, 16));
      button_panel.setLayout(new FlowLayout(FlowLayout.TRAILING));
      button_panel.add(spacer);
      button_panel.add(subReview);
      
      this.add(titleLabel);
      this.add(button_panel);
      this.add(authorLabel);
      this.add(new JPanel());
      this.add(rFilePanel);
      
      this.setMaximumSize(new Dimension(750, 80));
      this.setBorder(BorderFactory.createLineBorder(Color.BLACK)); 
    }
  }
  
  /**
   * Listener used for Submit/Edit Review Button.
   * Brings up a JFileChooser for the user to upload
   * a review document File to attach the paper.
   * 
   * @author Paul Schultz (ps87@uw.edu)
   * @version 12/02/2012
   */
  public class AddReviewListener extends AbstractAction
  {
    private Paper my_paper;
    private User my_reviewer;
    
    /**
     * Constructs a new AddReviewListener.
     * 
     * @param the_paper Paper to attach the review to.
     * @param the_user The reviewer adding the review.
     * @param the_frame master frame for the application.
     */
    public AddReviewListener(final Paper the_paper, final User the_user,
        final ConferenceWizardFrame the_frame)
    {
      my_paper = the_paper;
      my_reviewer = the_user;
    }
    
    /**
     * Bring up JFileChooser so the user can upload
     * a review File.
     */
    public void actionPerformed(ActionEvent arg0)
    {
      JFileChooser review = new JFileChooser();
      int choice = review.showOpenDialog(null);

      if (choice == JFileChooser.APPROVE_OPTION)
      {
        my_paper.editReview(my_reviewer, review.getSelectedFile());
        my_frame.setCurrentPanel(new ReviewerToolPanel(my_conference, my_user, my_frame));
      }
      else if (choice == JFileChooser.CANCEL_OPTION)
      {
        // Do Nothing & JFilechooser closes
      }
    }
  }
}
